package com.zhuhjay.boot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zhuhjay.constant.MessageConstant;
import com.zhuhjay.domain.entity.CheckItem;
import com.zhuhjay.domain.http.PageResult;
import com.zhuhjay.domain.http.QueryPageBean;
import com.zhuhjay.domain.http.Result;
import com.zhuhjay.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhuHJay
 * @date 2022/4/1 19:41
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @PostMapping
    public Result add(@RequestBody CheckItem checkItem){
        try{
            checkItemService.add(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @PostMapping("/pageQuery")
    public Result pageQuery(@RequestBody QueryPageBean queryPage){
        try{
            PageResult pageResult = checkItemService.queryPage(queryPage);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')") //权限校验
    public Result deleteItem(@PathVariable("id") Integer id){
        try{
            checkItemService.deleteItemById(id);
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @PutMapping
    public Result edit(@RequestBody CheckItem checkItem){
        try{
            checkItemService.edit(checkItem);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @GetMapping("/all")
    public Result findAll(){
        try{
            List<CheckItem> list = checkItemService.list();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
